package edu.fyvtc.javaweb.demo.aspect;


import edu.fyvtc.javaweb.demo.common.DemoResult;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class PracticeAspect {

    // within指定类名
    @Pointcut("within(edu.fyvtc.javaweb.demo.controller.AspectController)")
    public void allMethod() {

    }


    //execution指定返回类型，包名、类名、任意方法，任意入参
    @Pointcut("execution(java.lang.String edu.fyvtc.javaweb.demo.controller.AspectController.*(..))")
    public void returnStringMethod() {

    }


    //pointcut的运算
    @Pointcut("allMethod() && !returnStringMethod()")
    public void returnOthersMethod() {

    }


    //execution任意返回类型，包名、类名、任意方法名，第一个入参为House类
    @Pointcut("execution(* edu.fyvtc.javaweb.demo.controller.AspectController.*(edu.fyvtc.javaweb.demo.model.House, ..))")
    public void argsHouseMethod() {

    }


    //execution任意返回类型，包名、类名、方法名以meth开头，任意入参
    @Pointcut("execution(* edu.fyvtc.javaweb.demo.controller.AspectController.meth*(..))")
    public void startWithMethMethod() {

    }


    @SneakyThrows
    @Around("returnOthersMethod()")
    public DemoResult around(ProceedingJoinPoint proceedingJoinPoint) {
        log.info("around start");
        DemoResult demoResult = (DemoResult) proceedingJoinPoint.proceed();
        log.info("around end");
        return demoResult;
    }


    @Before("returnOthersMethod()")
    public void before() {
        log.info("Before");
    }


    @AfterReturning("returnOthersMethod()")
    public void afterReturning () {
        log.info("AfterReturning");
    }


    @AfterThrowing("returnOthersMethod()")
    public void afterThrowing() {
        log.info("AfterThrowing");
    }


    @After("returnOthersMethod()")
    public void brb() {
        log.info("After");
    }


}
